package com.wanghlme.day25.homework1;

import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public class DomForJavaParse {

    public static void main(String[] args) {
        String[] pojoInfoM = getFiledName(new ActionMapping());
        String[] pojoInfoR = getFiledName(new ActionResult());
        Element node = getDocument().getRootElement();
        System.out.println("XmlMap=" + getActionResultMapping(node.elements(), pojoInfoM, pojoInfoR, "Name"));

    }

    private static Document getDocument() {
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(new File("Day25/target/classes/struts.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return document;
    }

    private static String[] getFiledName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }

    private static Map<String, ActionResult> getActionResultMapping(List<Element> elementList, String[] fieldArray) {
        Map<String, ActionResult> map = new HashMap<>();
        for (Element element : elementList) {
            ActionResult ar = new ActionResult();
            ar.setPath(element.getTextTrim());
            List<Attribute> listAttr3 = element.attributes();
            for (Attribute attr : listAttr3) {
                String name = attr.getName();
                String value = attr.getValue();
                for (String s : fieldArray) {
                    if (s.equals(name)) {
                        Method m = null;
                        try {
                            String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
                            m = ar.getClass().getMethod(methodName, String.class);
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        }
                        try {
                            if (m != null) {
                                m.invoke(ar, value);
                            }
                        } catch (IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            map.put(ar.getName(), ar);
        }
        return map;
    }

    private static Map<String, ActionMapping> getActionResultMapping(List<Element> elementList, String[] fieldArray1, String[] fieldArray2, String str) {
        Map<String, ActionMapping> map = new HashMap<>();
        for (Element e1 : elementList) {
            ActionMapping am = new ActionMapping();
            List<Attribute> listAttr2 = e1.attributes();
            for (Attribute attr : listAttr2) {
                String name = attr.getName();
                String value = attr.getValue();
                for (int i = 0; i < fieldArray1.length; i++) {
                    if (fieldArray1[i].equals(name + str)) {
                        name += str;
                    }
                    if (fieldArray1[i].equals(name)) {
                        Method m = null;
                        try {
                            String mName1 = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
                            m = am.getClass().getMethod(mName1, String.class);
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        }
                        try {
                            if (m != null) {
                                m.invoke(am, value);
                            }
                        } catch (IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            List<Element> listElement2 = e1.elements();
            am.setResultMap(getActionResultMapping(listElement2, fieldArray2));
            map.put(am.getName(), am);
        }
        return map;
    }
}
